const fs = require("fs")
const path = require("path")


//readFile
fs.readFile(path.resolve('data.txt'), 'utf-8', (error, data) => {
  if (error) console.log(error)
  if (!error)
    console.log(data)
})



//writeFile
fs.writeFile('data.txt', 'hello world,rays77', {
  mode: 438,
  flag: 'r+',
  encoding: 'utf-8'
}, (error) => {
  if (!error)
    fs.readFile('data.txt', 'utf-8', (error, data) => {
      console.log(data)
    })
})

//appendFile
fs.appendFile('data.txt', '拉勾教育', (error) => {
  if (!error) console.log('写入成功')
  fs.readFile('data.txt', 'utf-8', (error, data) => {
    console.log(data)
  })
})

//copyFile
fs.copyFile('data.txt', 'test.txt', () => {
  console.log('拷贝成功')
  fs.readFile('test.txt', 'utf-8', (error, data) => {
    console.log(data)
  })
})

//watchFile
fs.watchFile('data.txt', { interval: 20 }, (cur, pre) => {
  if (cur.mtime !== pre.mtime) {
    console.log('文件被修改')
    fs.unwatchFile('data.txt')
  }
})